package com.fingard.dsp.bank.directbank.cmbc02.util;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.security.PrivateKey;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.cmbc02.util.cert.Cert;
import com.fingard.dsp.bank.directbank.cmbc02.util.cert.CertLoader;

import cmbc.cfca.sm2rsa.common.Mechanism;
import cmbc.cfca.sm2rsa.common.PKIException;
import cmbc.cfca.util.EnvelopeUtil;
import cmbc.cfca.util.SignatureUtil2;
import cmbc.cfca.util.cipher.lib.JCrypto;
import cmbc.cfca.util.cipher.lib.Session;
import cmbc.cfca.x509.certificate.X509Cert;

public class SM2Utils extends DirectBase{
	public static Session session = null;

	static {
		try {
			JCrypto.getInstance().initialize(JCrypto.JSOFT_LIB, null);
			session = JCrypto.getInstance().openSession(JCrypto.JSOFT_LIB);
		} catch (PKIException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 加签
	 * 
	 * @param merchantNum
	 * @param sourceData
	 * @return
	 * @throws PKIException
	 * @throws UnsupportedEncodingException
	 */
	public static  String signRaw(String merchantNum, String sourceData,PrivateKey privateKey) throws UnsupportedEncodingException, PKIException {
		
	
		return new String(new SignatureUtil2().p1SignMessage(Mechanism.SM3_SM2, sourceData.getBytes("UTF-8"),privateKey , session),"UTF-8");
	}
	
	/**
	 * 验签
	 * 
	 * @param sourceData
	 * @param signature
	 * @return
	 * @throws PKIException
	 * @throws UnsupportedEncodingException
	 */
	public static  boolean verifyRaw(String merchantNum, String sourceData, String signature,String cmbcPath) throws UnsupportedEncodingException, PKIException {
		
		
		Cert cmbcCert=null;
		try {
			cmbcCert=CertLoader.loadCmbcCert(cmbcPath,null,null);
		} catch (PlatformException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new SignatureUtil2().p1VerifyMessage(Mechanism.SM3_SM2, sourceData.getBytes("UTF-8"), signature.getBytes(), cmbcCert.cmbcPublicKey,
				session);
	}
	
	/**
	 * 加密
	 * 
	 * @param sourceData
	 * @return
	 * @throws PKIException
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("deprecation")
	public static String encrypt(String merchantNum, String sourceData,String certPath) throws UnsupportedEncodingException, PKIException {
		
		
					X509Cert signCert = null;
					String signAlg;
					
					try {
						signCert=CertLoader.loadCert(certPath);
						
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
						/*signCert = CertUtil.getCertFromSM2(pfxFileData);*/
					
					X509Cert[] certs={signCert};

		byte[] encryptedData = EnvelopeUtil.envelopeMessage(sourceData.getBytes("UTF-8"), Mechanism.SM4_CBC, certs);
		return new String(encryptedData, "UTF-8");
	}

	/**
	 * 解密
	 * 
	 * @param
	 * @param encryptedData
	 * @return
	 * @throws PKIException
	 * @throws UnsupportedEncodingException
	 */
	public static  String decrypt( String encryptedData,PrivateKey privateKey,String owncert) throws UnsupportedEncodingException, PKIException {
		
				
				X509Cert cert = null;
				try {
					cert = CertLoader.loadCert(owncert);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	
		byte[] sourceData = EnvelopeUtil.openEvelopedMessage(encryptedData.getBytes("UTF-8"), privateKey, cert,
				session);
		return new String(sourceData, "UTF-8");
	}
	
}
